import { deepMergeObject as deepMergeObjectInternalUse } from '@utils'

// 全屏缩放
export function initAutoScale(param) {
  var cwidth = param.cwidth
  var cheight = param.cheight
  var main = param.main
  var mainBox = param.mainBox
  var src = param.src
  var stretch = param.stretch

  function changesizefill() {
    var width = parseFloat($(window).outerWidth())
    var height = parseFloat($(window).outerHeight())
    var rwidth = width
    var rheight = height
    if (height > 1) {
      var crate = parseFloat(cwidth) / parseFloat(cheight)
      var rate = width / height
      var scale = 1
      var top = 0
      if (rate >= crate) {
        scale = (rheight / cheight)
      } else {
        scale = (rwidth / cwidth)
      }
      scale = Math.floor(scale * 1000) / 1000
      rwidth = scale * cwidth
      rheight = scale * cheight
      top = (height - rheight) / 2
      var scalestyle = 'scale(' + scale + ')'
      $(main).width(rwidth).height(rheight)
      $(mainBox).css('transform', scalestyle)
      $(mainBox).css('-moz-transform', scalestyle)
      $(mainBox).css('-webkit-transform', scalestyle)
      $(mainBox).css('-o-transform', scalestyle)
      $(main).css('margin-top', top)
    }
  }

  function changesizeuniform() {
    var rwidth = parseFloat($(window).outerWidth())
    var rheight = parseFloat($(window).outerHeight())
    var scalew = rwidth / cwidth
    var scaleh = rheight / cheight
    var scalestyle = 'scale(' + scalew + ', ' + scaleh + ')'

    $(main).width(rwidth).height(rheight)
    $(mainBox).css('transform', scalestyle)
    $(mainBox).css('-moz-transform', scalestyle)
    $(mainBox).css('-webkit-transform', scalestyle)
    $(mainBox).css('-o-transform', scalestyle)
  }

  function changesize() {
    if (stretch == 'fill') {
      changesizefill()
    } else {
      changesizeuniform()
    }
  }

  $(window).resize(changesize)

  changesize()
}

// 取指定位数随机数
export function getRandomId(len) { // 随机生成ID
  let num = ''
  for (let i = 0; i < len; i++) {
    num += Math.floor(Math.random() * 10)
  }
  return num
}

// 两个复杂对象深度合并
export function deepMergeObject(obj1, obj2) {
  const result = { ...obj2 }
  for (const key in obj1) {
    const value1 = obj1[key]
    const value2 = result[key]
    if (Object.prototype.hasOwnProperty.call(result, key)) {
      if (Object.prototype.toString.call(value1) === '[object Object]' && Object.prototype.toString.call(value2) === '[object Object]') {
        result[key] = deepMergeObjectInternalUse(value1, value2)
      }
    } else {
      result[key] = value1
    }
  }
  return result
}

// 匹配获取当前网络环境配置的网络类型
export function getCurrentNetworkSituation() {
  let currentNetworkSituation = {}
  for (const item of _const.deployUrlMark) {
    if (window.location.hostname.match(item.mark)) {
      currentNetworkSituation = item
      break
    }
  }
  // 根据系统访问网络情况设置网络类型
  let networkType = 0
  if (Object.keys(currentNetworkSituation).length) networkType = _const.deployUrlMarkType[currentNetworkSituation.type] // 若匹配到配置的特定网络，则获取该网路对应的类型
  return networkType // 返回值包含：0为互联网环境，其他值为全局配置参数deployUrlMark和deployUrlMarkType配置名称和配置类型
}

// 手机号脱敏
export function desensitizeTeL(tel) {
  if (tel === null) {
    return ''
  } else {
    if (tel.length === 11) {
      return tel.substr(0, 3) + '****' + tel.substr(7, 4)
    } else {
      return tel
    }
  }
}

// 身份证脱敏
export function desensitizeIDcard(id) {
  if (id === null) {
    return ''
  } else {
    if (id.length === 18) {
      return id.substr(0, 13) + '****'
    } else {
      return id
    }
  }
}

// 根据传入参数，计算组件缩放比例
export function componentSizeScale(componentOriginalDesignWidth = _const.screenOriginalDesignSize.width, componentOriginalDesignHeight = _const.screenOriginalDesignSize.height) {
  const componentOriginalDesignSize = {
    width: componentOriginalDesignWidth || _const.screenOriginalDesignSize.width,
    height: componentOriginalDesignHeight || _const.screenOriginalDesignSize.height
  }
  const scaleX = _const.screenOriginalDesignSize.width / componentOriginalDesignSize.width // 横向缩放比例
  const scaleY = _const.screenOriginalDesignSize.height / componentOriginalDesignSize.height // 竖向缩放比例
  const scaleRelative = scaleX < scaleY ? scaleX : scaleY // 相对缩放比例(取放大比例小者(或缩小比例大者)作为标准数值，用于无法区分在横向或纵向进行缩放时，例如高宽相同，按照不同横向竖向缩放会变形、圆角值缩放、线宽缩放等情况)
  const fontScaleTopLimit = 9 / 20 // 字体放大比例限制系数，当前为9/20
  const scaleFontRelative = scaleRelative <= 1 ? scaleRelative : scaleRelative * fontScaleTopLimit // 字体相对缩放比例(相对缩放比例大于1时，进行比例缩小放大比例数值，由于字体放大是整体(横向、竖向)放大，需进行回溯补偿，避免排版异常)
  return { scaleX, scaleY, scaleRelative, scaleFontRelative }
}
